package com.akcome.apollo.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.akcome.apollo.domain.DictionaryItem;
import com.akcome.apollo.repository.DictionaryItemRepository;
import com.akcome.apollo.service.DictionaryItemService;


/**
 * 字典表Service接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class DictionaryItemServiceImpl implements DictionaryItemService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private DictionaryItemRepository dictionaryItemRepository;
	
	@Override
	public DictionaryItem save(DictionaryItem dictionaryItem){
		return dictionaryItemRepository.save(dictionaryItem);
	}
	
	@Override
	public DictionaryItem findOne(Long id){
		return dictionaryItemRepository.findOne(id);
	}
	
	@Override
	public DictionaryItem findConfig(String code){
		List<DictionaryItem> dictionaryItems = dictionaryItemRepository.findByCode(code);
		if(CollectionUtils.isNotEmpty(dictionaryItems)){
			return dictionaryItems.get(0);
		}
		return null;
	}
	
	@Override
	public List<DictionaryItem> findDictionary(String code,String text){
		if(StringUtils.isBlank(text)){
			return dictionaryItemRepository.findByCode(code);
		}
		return dictionaryItemRepository.findByCodeAndTextLike(code,"%"+text+"%");
	}
}
